package com.example.week1;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.week1.adapter.MyAdapter;
import com.example.week1.entity.XiaEntity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    String url = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
    @BindView(R.id.rv)
    RecyclerView rv;
    List<XiaEntity.DataBean> list=new ArrayList<>();
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        rv.setLayoutManager(new LinearLayoutManager(this));

        //适配器
        myAdapter = new MyAdapter(R.layout.item,list);
        rv.setAdapter(myAdapter);
        //下划线
        rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        initData();
    }

    private void initData() {
        OkGo.<String>get(url)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();
                        XiaEntity xiaEntity = new Gson().fromJson(body, XiaEntity.class);
                        List<XiaEntity.DataBean> data = xiaEntity.getData();
                        list.addAll(data);
                        myAdapter.notifyDataSetChanged();
                    }
                });
    }

}
